UserInfo.init();
var gridListOne, gridListTwo, gridListThree, gridListFour;
var layform;
var id = UserInfo.userId; //当前操作者id
var oneindex = 0,twoindex = 0, threeindex = 0, fourindex = 0;
    $(document).ready(function () {
    layui.use(['form'], function() {
        layform = layui.form;
        var one = document.getElementById("one");
        var two = document.getElementById("two");
        var three = document.getElementById("three");
        var four = document.getElementById("four");


        gridListOne = new dataGrid("gridListOne");
        gridListTwo = new dataGrid("gridListTwo");
        gridListThree = new dataGrid("gridListThree");
        gridListFour = new dataGrid("gridListFour");

        initData();
        layform.on('radio(status)', function(data){
            switch (data.value) {
                case '1':
                    oneindex += 1;
                    one.style.display='block';
                    two.style.display='none';
                    three.style.display='none';
                    four.style.display='none';
                    gridListOne.reload({
                        'createUser':id,
                        "sysTaskKeyState": data.value
                    });
                    break;
                case '2':
                    twoindex += 1;
                    one.style.display='none';
                    two.style.display='block';
                    three.style.display='none';
                    four.style.display='none';
                    if(twoindex == 1){
                        gridListTwo.load({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }else{
                        gridListTwo.reload({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }
                    break;
                case '3':
                    threeindex += 1;
                    one.style.display='none';
                    two.style.display='none';
                    three.style.display='block';
                    four.style.display='none';
                    if(threeindex == 1){
                        gridListThree.load({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }else{
                        gridListThree.reload({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }
                    break;
                case '4':
                    fourindex += 1;
                    one.style.display='none';
                    two.style.display='none';
                    three.style.display='none';
                    four.style.display='block';
                    if(fourindex == 1){
                        gridListFour.load({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }else{
                        gridListFour.reload({
                            'createUser':id,
                            "sysTaskKeyState": data.value
                        });
                    }
                    break;
            }

        });
    });
});

function initData() {
    gridListOne.load({
            'createUser':id,
            'sysTaskKeyState':1
    });
}

function formatOper(e) {
    var row = e.record;
    return "<a class='layui-btn layui-btn-sm' hideFocus='' href='javascript:void(0)' onClick='open_Detail(\"" + row.id + "\",\"" + row.wfId + "\")'><i class='layui-icon'>&#xe629;</i>查看</a>"
        +"<a class='layui-btn layui-btn-sm' hideFocus='' href='javascript:void(0)' onClick='retrieveTask(\"" + row.id + "\")'><i class='layui-icon'>&#xe629;</i>取回</a>";
}

function formatByTwo(e) {
    var row = e.record;
    return "<a class='layui-btn layui-btn-sm' hideFocus='' href='javascript:void(0)' onClick='open_Detail(\"" + row.id + "\",\"" + row.wfId + "\")'><i class='layui-icon'>&#xe629;</i>查看</a>";
}

function open_Detail(baseTaskId, wfId) {
    showProgressView(wfId, baseTaskId, "", "","", 'viewTimeline.html', 'viewFlowchat.html', 'viewForm.html');
    //gridList.reload();
}

function retrieveTask(id) {
    ActHttpRequest("sysTaskKey!retrieveTask.do", {id:id}, function (data) {
        if (data.flag) {
            layer.msg('取回流程失败！已在审批中！', {
                time: 2000, //20s后自动关闭
                btn: ['OK']
            });
            return;
        }
        else{
            layer.msg('取回成功！', {
                time: 2000, //20s后自动关闭
                btn: ['OK']
            });
            return;
        }
    }, null, null, null, '加载中。。。');
    gridList.reload();
}


function formatModuleId(e) {
    var row=e.record;
    var moduleName = "";
    ActHttpRequest("/wf/module/findById", {"id":row.moduleId}, function (d) {
        moduleName =  d.wfModule.moduleName;
    });
    return moduleName;
}

function formatCurrentSysTaskId(e) {
    var row=e.record;
    var activityName = "";
    ActHttpRequest("/wf/systask/findById", {"id":row.currentSysTaskId}, function (d) {
        ActHttpRequest("/wf/activity/"+d.wfSystask.activityId, {}, function (da) {
            activityName =  da.wfActivity.activityName;
        });
    });
    return activityName;
}

function formatSysTaskKeyState(e) {
    var row=e.record;
    var state = "";
    switch (row.sysTaskKeyState) {
        case 1:
            state="办理中";
            break;
        case 2:
            state="已办结";
            break;
        case 3:
            state="被撤销";
            break;
        case 4:
            state="已取回";
            break;
    }
    return state;
}


function formatTitle(e) {
    var row = e.record;
    console.log(row)
    var t = "";
    var wfTitle = row.wfTitle;
    var record ;
    ActHttpRequest("/wf/systask/findById",{"id": row.currentSysTaskId}, function (data) {
       if(data.OK == "OK"){
           record = JSON.parse(data.wfSystask.sysTaskRecord.replace(/\'/g,"\""));
           console.log(record)
           if(record == "" || record == undefined){
               t="";
           }else{
               console.log(wfTitle)
               var index_1 = wfTitle.indexOf("{{"),
                   index_2 = wfTitle.indexOf("}}");
               var firstTitle = wfTitle.substring(0, index_1);
               var secondTitle = wfTitle.substring(index_2 + 2);
               var title = wfTitle.substring(index_1 + 2, index_2);

               record.forEach(function (data) {
                   if(data.name == title){
                       t = data.values[0];
                   }
               });
               t = firstTitle + t + secondTitle;
           }
       }
    });

    return t;
}